<?php

namespace App\Http\Controllers;

use App\Models\BranchCourt;
use App\Models\Dynamic;
use Illuminate\Http\Request;

class BranchCourtController extends Controller
{
    /**
     * @return mixed
     * 查询医院分院列表
     */
    function branchCourt(){
        return BranchCourt::get()->toArray();
    }

    /**
     * @param Request $request
     * @return mixed
     * 分院动态接口
     */
    function branchDynamic(Request $request){
        return Dynamic::where('hospital_id',$request->get('id'))->get()->toArray();
    }

    /**
     * @param Request $request
     * @return mixed
     * 医院详情
     */
    function branchCourtInfo(Request $request){
        return BranchCourt::where('id',$request->get('id'))->first()->toArray();
    }

    /**
     * @param Request $request
     * 地图驾车导航
     */
    function mapBranch(Request $request){
        //获取用户当前ip位置
        $ip=$_SERVER['REMOTE_ADDR'];
        $ipAddress=json_decode(file_get_contents("https://restapi.amap.com/v3/ip?ip=$ip&output=json&key=2abb737e70127ff9d2ee7958f6091b9c"),true);
        $atPresent=$request->get('at_address');
        $hospitalAddress=BranchCourt::where('id',$request->get('id'))->value('hospital_address');
        //获取当前位置的经纬度
        $origin=file_get_contents("https://restapi.amap.com/v3/geocode/geo?address=$atPresent&output=json&key=2abb737e70127ff9d2ee7958f6091b9c");
        $location=json_decode($origin,true)['geocodes'][0]['location'];
        //获取目标位置的经纬度
        $origin=file_get_contents("https://restapi.amap.com/v3/geocode/geo?address=$hospitalAddress&output=json&key=2abb737e70127ff9d2ee7958f6091b9c");
        $destination=json_decode($origin,true)['geocodes'][0]['location'];
        //当前位置至目标位置的公交通行路线
        $data=file_get_contents("https://restapi.amap.com/v5/direction/driving?origin=$location&destination=$destination&output=json&key=2abb737e70127ff9d2ee7958f6091b9c");
        return json_decode($data,true);
    }
}
